<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data.Common" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.Windows.Forms" %>
<%@ import Namespace="System.Reflection" %>

<script runat="server">
    
void Page_Load() {
    // Set content type
    Request.ContentType = "text/plain";
    
    StreamReader sReader = new StreamReader(Request.InputStream);
    String FirstLine = sReader.ReadLine();
    String FileName = ConfigurationManager.AppSettings["PageTitle"] + " - " + FirstLine + ".csv";
    String ExportPath = Server.MapPath("Exports");

    String FilePath = ExportPath + "\\" + FileName;

    if (File.Exists(FilePath))
    {
        File.Delete(FilePath);
    }
        
    FileStream fsDocFile = File.Open(FilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
    StreamWriter swDocFile = new StreamWriter(fsDocFile);
    swDocFile.WriteLine(FirstLine);
    swDocFile.Write(sReader.ReadToEnd());

    sReader.Close();
    swDocFile.Close();

    Response.Write(FileName);

    
    
    
    //String CurrentFilePath = Request.PhysicalPath;
    //String AppPath = Convert.ToString(Directory.GetParent(CurrentFilePath));
    //String ExportPath = AppPath + "\\" + "Exports";
    //String ExportPath = ConfigurationManager.AppSettings["ProjectPath"] + "\\" + "Exports";
    //String FileName = "ExportCSV.csv";  // wgo - generate automatically or import, delete when done
    //String FileName = Convert.ToString(new Random(DateTime.Now.Millisecond));
    //String FileName = FirstLine + ".csv";
    
}
</script>